document.getElementById("createUnit").onclick=function(){
	window.open ("jizhangUnit2.html?action=admin&type=POST", "_blank", "height=400, width=600, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
}
function deleteUser(e){
	var data={}
	data.username=e.getAttribute("username")
	var status=e.getAttribute("status")
	if (status==="true"){
		data.status=false
	}
	else{
		data.status=true
	}
	var _data=JSON.stringify(data)
	if(!confirm("真的要更改吗？")){
		return false;
	}
	$.ajax({
		"url":"/user_admin/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"type":"DELETE",
		"complete":stop_load_pic,
		"data":{"data":_data},
		"success":function(return_data){
			if(responseCheck(return_data)){
				showSuccessNotice()
				e.setAttribute("status",data.status)
				if (data.status===true){
					e.className="btn btn-xs btn-success"
					e.textContent="正常"
				}
				else{
					e.className="btn btn-xs btn-danger"
					e.textContent="已停用"
				}
			}
		}
		
	})
	
}
function createLine(data){
				for(var i=0;i<data.content.length;i++){
					var tab=document.getElementById("tab")
					var tr=document.createElement("tr")

					var td=document.createElement("td")
					td.textContent=jiami(data.content[i].username,data.user_type)
					tr.appendChild(td)

					var td=document.createElement("td")
					td.textContent=data.content[i].number
					tr.appendChild(td)


					var td=document.createElement("td")
					td.textContent=data.content[i].site
					tr.appendChild(td)

					var td=document.createElement("td")
					td.textContent=data.content[i].user_type
					tr.appendChild(td)

					var td=document.createElement("td")
					td.textContent=Math.abs(data.content[i].banance)
					tr.appendChild(td)

					var td=document.createElement("td")
					td.textContent=data.content[i].settled_fee
					tr.appendChild(td)

					var td=document.createElement("td")
					td.textContent=data.content[i].date
					tr.appendChild(td)

					var td=document.createElement("td")
					var b=document.createElement("button")
					b.setAttribute("username",data.content[i].username)
					b.setAttribute("status",data.content[i].status)
					b.onclick=function(){
						deleteUser(this)
					}
					if(data.content[i].status===false){
						b.className="btn btn-xs btn-danger"
						b.textContent="已停用"
					}
					else{
						b.className="btn btn-xs btn-success"
						b.textContent="正常"
					}
					
					td.appendChild(b)
					tr.appendChild(td)

					var td=document.createElement("td")
					if (data.user_type==="财务"){
					var b=document.createElement("button")
					b.textContent="编辑"
					b.className="btn btn-xs btn-primary"
					b.setAttribute("data",JSON.stringify(data.content[i]))
					b.onclick=function(){
						var data=this.getAttribute("data")
						window.open ("jizhangUnit2.html?action=admin&type=PUT&data="+data, "_blank", "height=400, width=600, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
					}
					td.appendChild(b)

					var b=document.createElement("button")
					b.textContent="重置密码"
					b.style.marginLeft="10px"
					b.className="btn btn-xs btn-info"
					b.setAttribute("data",JSON.stringify(data.content[i]))
					b.onclick=function(){
						var data=this.getAttribute("data")
						data=JSON.parse(data)
						var username=data.username
						window.open ("password.html?username="+username, "_blank", "height=400, width=600, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
					}
					td.appendChild(b)

					}

					var b=document.createElement("button")
					b.className="btn btn-warning btn-xs"
					b.setAttribute("username",data.content[i].username)
					b.setAttribute("number",data.content[i].number)
					b.textContent="记账清单"
					b.style.marginLeft="10px"
					b.onclick=function(){
						var username=this.getAttribute("username")
						var number=this.getAttribute("number")
						window.open ("billList.html?username="+username+"&number="+number, "_blank", "height=800, width=1400, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
					}

					td.appendChild(b)
					tr.appendChild(td)

					var b=document.createElement("button")
					b.className="btn btn-danger btn-xs"
					b.setAttribute("username",data.content[i].username)
					b.setAttribute("unit",data.content[i].number)
					b.textContent="车辆管理"
					b.style.marginLeft="10px"
					b.onclick=function(){
						var username=this.getAttribute("username")
						var unit=this.getAttribute("unit")
						window.open ("vehicle.html?username="+username+"&unit="+unit, "_blank", "height=800, width=1400, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
					}

					td.appendChild(b)
					tr.appendChild(td)
					tab.appendChild(tr)
			}
}
function getList(){
	$.ajax({
		"url":"/user_admin/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"dataType":"jsonp",
		"success":function(data){
			if(responseCheck(data)){
				createLine(data)
			}
		}
	})
}
function query(data){
	$("#tab>tr").remove()
	data.action="admin"
	data=JSON.stringify(data)
	$.ajax({
		"url":"/user_admin/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"data":{"data":data},
		"dataType":"jsonp",
		"success":function(data){
			if(responseCheck(data)){
				createLine(data)
			}
		}
	})
}
//getList()

query({"username":".*","user_type":"记账单位"})
